package com.lost.octopus.es.processor.web.controller.base;

import com.fasterxml.jackson.core.type.TypeReference;
import com.sunsharing.share.common.base.exception.ShareBusinessException;
import com.sunsharing.share.common.base.exception.ShareResponseCode;
import com.sunsharing.share.common.mapper.JsonMapper;
import com.lost.octopus.es.processor.entity.IdEntity;
import com.lost.octopus.es.processor.feign.Api;
import com.lost.octopus.es.processor.scheduler.EsSyncIndexOperator;
import com.lost.octopus.es.processor.service.common.BaseEsServiceImpl;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * 基础服务接口，用于抽象通用基础服务接口
 * 默认使用对应的 {@link BaseEsServiceImpl} 进行查询
 * @author zhangbh
 **/
@Log4j2
public class BaseServiceController<S extends BaseEsServiceImpl<E, I, ?>, E extends IdEntity<I>, I> extends BaseController
        implements Api<E, I> {

    @Autowired(required = false)
    protected S service;

    @Autowired(required = false)
    EsSyncIndexOperator<E, I, ?, ?> operator;


    /**
     * 根据ID查询数据
     * @param id ID
     * @return 数据
     */
    @GetMapping(value = "findById.do", name = "ID查询")
    public E findById(@RequestParam I id) {
        return service.findById(id);
    }

    /**
     * 根据ID批量查询数据
     * @param ids ids
     * @return 数据
     */
    @PostMapping(value = "findAllById.do", name = "ID批量查询")
    public Iterable<E> findAllById(@RequestBody List<I> ids) {
        return service.findAllById(ids);
    }

    /**
     * 重载索引
     * @param ids ids
     * @return 同步数据量
     */
    @PostMapping(value = "reloadById.do", name = "批量重载索引")
    public int reloadById(@RequestBody List<I> ids) {
        List<String> strIds = JsonMapper.nonNullMapper().getMapper().convertValue(ids, new TypeReference<ArrayList<String>>(){});
        if (operator == null) {
            log.error("此索引不支持reloadById操作，请检查具体的数据库查询实现,当前类={}", this.getClass().getName());
            throw new ShareBusinessException(ShareResponseCode.INNER_SYSTEM_ERROR.getCode(), "此索引不支持reloadById操作！！！");
        }
        return operator.reloadByIds(strIds);
    }



    /**
     * 根据ID保存数据，null字段不更新
     * @param save 保存对象
     */
    @PostMapping(value = "saveOrUpdate.do", name = "更新")
    public void saveOrUpdate(@Valid @RequestBody E save) {
        service.saveOrUpdate(save);
    }

    /**
     * 根据ID批量保存，null字段不更新
     * @param list
     */
    @PostMapping(value = "batchSaveOrUpdate.do", name = "批量更新")
    public void batchSaveOrUpdate(@Valid @RequestBody List<E> list) {
        service.batchSaveOrUpdate(list);
    }

    /**
     * 通过ID删除
     * @param id id
     */
    @DeleteMapping(value = "deleteById.do", name = "删除")
    public void deleteById(@RequestParam I id) {
        service.deleteById(id);
    }

    /**
     * 通过ID批量删除
     * @param ids ids
     */
    @DeleteMapping(value = "deleteAllById.do", name = "批量删除")
    public void deleteAllById(@RequestBody @Valid List<I> ids) {
        service.deleteAllById(ids);
    }
}
